// pages/kanjia/join/index.js
var app = getApp(),
  a = app.requirejs("core"),
  b = app.requirejs("api/index");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    gray_star: "/yb_mingpian/static/images/gray-star.png",
    light_star: "/yb_mingpian/static/images/light-star.png",
    store_pic_array: [],
    score: [1, 1, 1, 1, 1],
    total_score: 0,
    url: getApp().globalData.api,
    info: {},
    form_data: {},
    data:{},
  },
  score: function (e) {
    var that = this,
      index = a.pdata(e).index,
      total_score = (index + 1),
      arr = [1, 1, 1, 1, 1];
    for (var i in arr) {
      if (i <= index) {
        arr[i] = 2
      }
    }
    that.setData({ score: arr, total_score: total_score })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (op) {
    console.log(op,'99');
    var t = op.type, k = op.key, v = op[k], c = op.clo_name, g = op.goods_ids;
    this.setData({
      "info.t": t,
      "info.c": c,
      "info.k": k,
      "info.v": v,
      "info.g": g
    })
  },

  /**
   * 提交【评论
   */
  formSubmit: function (e) {
    var that = this;
    var score = that.data.total_score;
    var formData = e.detail.value;
    if (formData.info == '') {
      a.alert('内容不能为空！');
      return;
    }
    if (score == 0) {
      a.alert('您还没有评分哦');
      return;
    }
    formData.fraction = score;
    formData.user_id = getApp().getCache("userinfo").uid;
    if (Object.keys(that.data.data).length > 0) {
      formData.array_pic = JSON.stringify(that.data.data.comm);
    }
    formData.t1 = that.data.info.t;
    formData.k1 = that.data.info.k;
    formData.v1 = that.data.info.v;
    formData.c1 = that.data.info.c;
    formData.g1 = that.data.info.g;
    formData.p1 = that.data.info.p;
    
    a.post('Arliki/write_comment',
      formData, function (t) {
        console.log(t)
        if (t.code == 1) {
          //成功操作
          a.alert('提交成功', function () {
            a.jump('/yb_mingpian/pages/member/index/index', 3);
          })
        } else {
          //失败提示
          a.alert(t.msg)
        }
      })
  },
  chooseImageTap: function () {
    if (Object.keys(this.data.form_data).length >= 9) {
      a.alert("最多上传9张图");
    } else {
      b.upload(this, "comm", 'pinglun', 0);
    }
  },
  Image_del: function (e) {
    var that = this,
      index = a.pdata(e).index,
      arr = that.data.form_data["comm"];
    a.removeByValue(arr, index, function (i) {
      that.setData({
        'form_data.comm': i
      })
    })
  },

})